package com.rj2002.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.rj2002.pojo.User;

public class LoginInterceptor implements HandlerInterceptor{

	public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
		HttpSession session = request.getSession();
		response.setCharacterEncoding("UTF-8");
		User user = (User)session.getAttribute("account");
		String url = request.getServletPath();
		if (user == null) {
			response.sendRedirect("/rj2002/user/login");
			return false;
		} else {
			//判断权限
			if(url.contains("admin") && user.getRole() != 2) {
				return false;
			}else if(url.contains("sale") && user.getRole() != 1){
				return false;
			}else if(url.contains("buy") && user.getRole() != 0){
				return false;
			}
			return true;
		}
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {	
	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
	}
}
